<? if (!defined('BASEPATH')) exit('No direct script access allowed');

class PostsModel extends CI_Model {

    public function getPosts($post_id) {
        $this->db->select("*, posts.id as post_id, UNIX_TIMESTAMP(posts.date_posted) as date_posted_timestamp");
        $this->db->from("posts");
        if (isset($post_id)) {
            $this->db->where("posts.id ", $post_id);
        }
        $this->db->join("users", "users.id = posts.user_id", "left");
        $this->db->join("messages", "messages.id = posts.message_id", "left");
        $this->db->join("polls", "polls.id = posts.poll_id", "left");
        $this->db->join("events", "events.id = posts.event_id", "left");
        $this->db->order_by("date_posted", "DESC");
        $query = $this->db->get();

        $result = $query->result();
        $this->load->model('PollsModel');
        $posts = $this->PollsModel->addPollOptionsToPosts($result);

        if (isset($post_id)) {
            return $posts[0];
        } else {
            return $posts;
        }
    }

    public function insertPost($type, $message_id, $user_id) {
        // First determine recent avatar filename
        $this->db->select("filename");
        $this->db->from("avatars");
        $this->db->join("images", "images.id = avatars.image_id");
        $this->db->where("avatars.user_id", $user_id);
        $this->db->order_by("images.date_uploaded", "DESC");
        $this->db->limit(1);
        $query = $this->db->get();

        $result = $query->result();
        $filename = $result[0]->filename;

        // Now insert the post
        $data = array("type" => $type,
                      "user_id" => $user_id,
                      "avatar_filename" => $filename);
        if (isset($message_id)) {
            $data[$type."_id"] = $message_id;
        }
        $this->db->insert("posts", $data);
    }

    public function deletePost($post_id) {
        $this->db->select("*");
        $this->db->from("posts");
        $this->db->where("id", $post_id);

        $query = $this->db->get();
        $result = $query->result();
        $post = $result[0];

        $this->deletePosts($post_id);

        // echo $post->type;
        if ($post->type == "message") {
            $this->deleteMessages($post->message_id);
        } elseif ($post->type == "poll") {
            $this->deleteUserPollOptions($post->poll_id);
            $this->deletePollOptions($post->poll_id);
            $this->deletePoll($post->poll_id);
        }
    }

    public function oldAvatarFilename($user_id, $current_filename) {
        $this->db->select("date_uploaded");
        $this->db->from("images");
        $this->db->where("filename", $current_filename);
        $query = $this->db->get();
        $results = $query->result();
        $date_uploaded = $results[0]->date_uploaded;

        $this->db->select("*");
        $this->db->from("images");
        $this->db->join("avatars", "avatars.image_id = images.id", "left");
        $this->db->where("user_id", $user_id);
        $this->db->where("date_uploaded < '$date_uploaded'");
        $this->db->order_by("date_uploaded", "DESC");
        $this->db->limit(1);

        $query = $this->db->get();
        $results = $query->result();
        $image = $results[0];
        return $image->filename;
    }

    // =========================================== Private helper functions ============================================

    private function deletePosts($post_id) {
        $this->db->where('id', $post_id);
        $this->db->delete('posts');
    }

    private function deletePoll($poll_id) {
        $this->db->where('id', $poll_id);
        $this->db->delete('polls');
    }

    private function deletePollOptions($poll_id) {
        $this->db->where('poll_id', $poll_id);
        $this->db->delete('poll_options');
    }

    private function deleteMessages($messageId) {
        $this->db->where('id', $messageId);
        $this->db->delete('messages');
    }

    private function deleteUserPollOptions($poll_id) {
        $this->db->select("id");
        $this->db->from("poll_options");
        $this->db->where("poll_id", $poll_id);

        $query = $this->db->get();
        $result = $query->result();

        foreach ($result as $poll_option) {
            $this->db->where('poll_option_id', $poll_option->id);
            $this->db->delete('user_poll_options');
        }
    }
}